package com.wanxi.education.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanxi.education.mapper.RoleMapper;
import com.wanxi.education.mapper.UserMapper;
import com.wanxi.education.model.pojo.User;
import com.wanxi.education.model.vo.user.*;
import com.wanxi.education.service.AUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @Author: YanZhengYi
 * @Description:
 * @Date: Created in 2021/9/24
 * @Version: 1.0
 */
@Service
public class AUserServiceImpl implements AUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public PageInfo<AUserReturnVo> aUserFindPage(AUserVo aUserVo) {
        PageHelper.startPage(aUserVo.getPageNum(),aUserVo.getPageSize());
        List<AUserReturnVo> aUserReturnVoList = userMapper.aUserFindPage(aUserVo);
        //遍历该集合,拿到UserId去查询对应id的所有的角色，再将角色赋值给返回Vo
        for (AUserReturnVo aUserReturnVo : aUserReturnVoList) {
            Set<RoleVO> byUserId = roleMapper.findByUserId(aUserReturnVo.getId());
            aUserReturnVo.setRole(byUserId);
        }
        return new PageInfo<>(aUserReturnVoList);
    }

    @Override
    @Transactional
    public Integer aUserUpdate(UpdateAUserVo updateAUserVo) {
        String password = updateAUserVo.getPassword();
        if (password==null){
            User byId = userMapper.findById(updateAUserVo.id);
            updateAUserVo.setPassword(byId.getPassword());
        }
        try {
            //新密码过来加密
            updateAUserVo.setPassword(passwordEncoder.encode(updateAUserVo.getPassword()));
            Integer result = userMapper.aUserUpdate(updateAUserVo);
            if (result!=1){
                return null;
            }
            return result;
        }catch(Exception e){
            return null;
        }
    }

    @Override
    public Integer AdminDelete(Long id) {
        return userMapper.deleteAdmin(id);
    }

    @Override
    public FindByIdAUserVo findById(Long id) {
        User user = userMapper.findById(id);
        if (user==null)return null;
        //密码不需要回显，需要重新输入
        FindByIdAUserVo findByIdAUserVo = new FindByIdAUserVo();
        BeanUtils.copyProperties(user, findByIdAUserVo);
        return findByIdAUserVo;
    }

    @Override
    @Transactional
    public Integer aUserInsert(UpdateAUserVo updateAUserVo) {

        String phone = updateAUserVo.getPhone();
        User user = userMapper.findByPhoneAndStatusAndId(phone);
        updateAUserVo.setPassword(passwordEncoder.encode(updateAUserVo.getPassword()));
        if (user!=null){
            //判断有密码，则加密新密码
            userMapper.aUserUpdate(updateAUserVo);
        }
        try {
            Integer result = userMapper.aUserInsert(updateAUserVo);
            if (result!=1){
                return null;
            }
            return result;
        }catch (Exception e){
            return null;
        }

    }

//    首先删除关联表中所有的相关数据，再插入所有的角色关联
    @Override
    @Transactional
    public Integer updateRole(UpdateRoleVo updateRoleVo) {
        userMapper.deleteRole(updateRoleVo.getId());
        return userMapper.insertRole(updateRoleVo.getRoleId(),updateRoleVo.getId());
    }
}
